package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.EcTemplateElementStyleRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.EcTemplate;
import com.ndp.fb.rdb.model.ectemplate.EcTemplateElementStyle;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by sisi.chen on 2016/7/12.
 */
@Service
public class EcTemplateElementStyleRdbServiceImpl extends DaoTemplate implements EcTemplateElementStyleRdbService {
    @Override
    public Long save(EcTemplateElementStyle ecTemplateElementStyle) {
        return saveResultId(ecTemplateElementStyle);
    }

    @Override
    public List<EcTemplateElementStyle> findByTemplateId(Long templateId) {
        String sql = "select * from ec_template_element_style where template_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{templateId}, DefaultRowMapper.autoRowMapper(EcTemplateElementStyle.class));
    }

    @Override
    public EcTemplateElementStyle findByTemplateIdAndElementStyleGroupId(Long templateId,int elementStyleGroupId) {
        String sql = "select * from ec_template_element_style where template_id = ? and element_style_group_id = ?";
        return findOne(sql, new Object[]{templateId,elementStyleGroupId}, DefaultRowMapper.autoRowMapper(EcTemplateElementStyle.class));
    }

    @Override
    public List<Long> batchSave(List<EcTemplateElementStyle> needBindedGroups) {
        return batchSaveResultId(needBindedGroups);
    }

    @Override
    public int[] batchDelete(List<EcTemplateElementStyle> ecTemplateElementStyleList) {
        String sql = "DELETE FROM ec_template_element_style  WHERE template_id = ? AND element_style_group_id = ?";
        return getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter(){
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                ps.setLong(1,ecTemplateElementStyleList.get(i).getTemplateId());
                ps.setLong(2,ecTemplateElementStyleList.get(i).getElementStyleGroupId());
            }
            public int getBatchSize() {
                return ecTemplateElementStyleList.size();
            }
        });
    }

    @Override
    public List<EcTemplate> findTemplateByStyleGroupIdAndOrgId(int groupId, Long orgId) {
        String sql = "SELECT DISTINCT t.id,t.template_name,t.template_img FROM ec_template t INNER JOIN (SELECT t2.* FROM ec_template_element_style t2 " +
                "WHERE t2.element_style_group_id = ?) t3 ON t.`id` = t3.template_id WHERE t.is_delete = 0 and t.org_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{groupId, orgId}, DefaultRowMapper.autoRowMapper(EcTemplate.class));
    }

    @Override
    public List<EcTemplate> findByStyleGroupIdsAndOrgId(Set<Integer> styleGroups, Long orgId) {

        StringBuilder sb = new StringBuilder();
        sb.append("select distinct t.* from ec_template t,ec_template_element_style ts ");
        sb.append("where t.id = ts.template_id and is_delete = 0 and t.org_id = ? ");
        List<Object> params = new ArrayList<>();
        params.add(orgId);
        if (!CollectionUtils.isEmpty(styleGroups)){
            sb.append( " and ts.element_style_group_id in (");
            boolean isFirst = true;
            for (Integer styleGroup : styleGroups){
                if (!isFirst){
                    sb.append(",");
                }
                sb.append("?");
                isFirst = false;
                params.add(styleGroup);
            }
            sb.append(")");

        }

       return getJdbcTemplate().query(sb.toString(), params.toArray(), DefaultRowMapper.autoRowMapper(EcTemplate.class));
    }
}
